#!usr/bin/env python  
# -*- coding:utf-8 -*-
""" 
@author:robot
@file: title02开心消消乐.py 
@version:
@time: 2023/05/27

https://dream.blog.csdn.net/article/details/129045100
"""
import sys


def solve_method(n: int, m: int, matrix: [[int]]) -> None:
    times = 0
    for x in range(n):
        for y in range(m):
            cur = matrix[x][y]
            if cur == 1:
                times += 1
                changed = []
                changed.append([x, y])
                change(n, m, matrix, changed)
    print(times)


def change(n: int, m: int, matrix: [[int]], changed: [[int]]) -> None:
    if not changed:
        return

    ints = changed.pop(0)
    directions = [(-1, -1), (0, -1), (1, -1), (-1, 0), (1, 0), (-1, 1), (0, 1), (1, 1)]

    for d in directions:
        newX = ints[0] + d[0]
        newY = ints[1] + d[1]
        if 0 <= newX < n and 0 <= newY < m and matrix[newX][newY] == 1:
            matrix[newX][newY] = 0
            changed.append([newX, newY])
    change(n, m, matrix, changed)


if __name__ == '__main__':
    n, m = map(int, input().split())
    matrix = []
    for x in range(n):
        row = list(map(int, input().split()))
        matrix.append(row)
    solve_method(n, m, matrix)
